import FileSaver from 'file-saver'
import * as XLSX from 'xlsx'
import { Message } from 'element-ui'

// 封装时间格式化
export const getInitTime = (toDay, yy, mm, dd, hh, mu, ss) => {
  // let today = toDay || new Date()
  let year = toDay.getFullYear() //年
  let month =
    toDay.getMonth() + 1 < 10
      ? '0' + (toDay.getMonth() + 1)
      : toDay.getMonth() + 1 //月
  let data = toDay.getDate() < 10 ? '0' + toDay.getDate() : toDay.getDate() //日
  let hour = toDay.getHours() < 10 ? '0' + toDay.getHours() : toDay.getHours() //时
  let minute =
    toDay.getMinutes() < 10 ? '0' + toDay.getMinutes() : toDay.getMinutes() //分
  let second =
    toDay.getSeconds() < 10 ? '0' + toDay.getSeconds() : toDay.getSeconds() //秒

  const nowTime =
    year +
    `${yy ? yy : '-'}` +
    month +
    `${mm ? mm : '-'}` +
    data +
    `${dd ? dd : ' '}` +
    hour +
    `${hh ? hh : ':'}` +
    minute +
    `${mu ? mu : ':'}` +
    second +
    `${ss ? ss : ''}`
  return nowTime
}

// 封装前端导出
export const exportInitExcel = (excel, xlsxName) => {
  var wbout = XLSX.write(excel, {
    bookType: 'xlsx',
    bookSST: true,
    type: 'array',
  })
  try {
    FileSaver.saveAs(
      new Blob([wbout], { type: 'application/octet-stream' }),
      // 设置导出文件名
      `${xlsxName}`
    )
  } catch (e) {
    if (typeof console !== 'undefined') console.log(e, wbout)
  }
  return wbout
}

// 封装后端导出
export const exportUrlExcel = (excel, xlsxName) => {
  let url = window.URL.createObjectURL(
    new Blob([excel], {
      type: 'application/vnd.ms-excel',
    })
  )
  if ('download' in document.createElement('a')) {
    const a = document.createElement('a')
    a.href = url
    a.download = xlsxName
    a.style.display = 'none'
    document.body.appendChild(a)
    a.click()
    URL.revokeObjectURL(a.href)
    document.body.removeChild(a)
  }
  return
}

// 封装模糊搜索 搜索高亮
export const searchBlurry = (text, keyword, highlightStr) => {
  if ((text, keyword, highlightStr)) {
    let highlightStr = `<span class=${highlightStr}>${keyword}</span>`
    const reg = new RegExp(keyword, 'gi')
    return text.replace(reg, highlightStr)
  }
}

// 封装复制到粘贴板
export const copyContent = (val) => {
  var cInput = document.createElement('input')
  cInput.value = val
  document.body.appendChild(cInput)
  cInput.select() // 选取文本框内容
  document.execCommand('copy')
  document.body.removeChild(cInput)
  Message.success('复制成功')
  return
}

// 封装防抖
export const debounce = (fnName, time) => {
  let timeout = null
  return function () {
    if (timeout) clearTimeout(timeout)
    timeout = setTimeout(() => {
      this[fnName]()
    }, time)
  }
}

// 封装节流
export const throttle = (click, time) => {
  let timeout = null
  return function (click, time) {
    if (!timeout) {
      timeout = setTimeout(() => {
        this[click]()
        timeout = null
      }, time)
    }
  }
}

// 封装根据条件查询
export const dictCheckBayKey = (data, keys) => {
  let arr = {}
  data.forEach((item) => {
    let key = item[keys]
    if (!arr[key]) arr[key] = item
  })
  return arr
}

// 封装26个字母
export const getWord = (num) => {
  if (num > 27) return
  var wordList = []
  for (var i = 0; i < num; i++) {
    wordList.push(String.fromCharCode(97 + i))
  }
  return wordList
}

// 获取随机数并去重
export const getRnumRepetition = (num) => {
  if (num == null || num == 0) return
  var arr = []
  for (let i = 0; i < num; i++) {
    arr.push(Math.floor(Math.random() * (i - 1) + 1))
  }
  // 去重
  let numIndex = [...new Set(arr)]
  return numIndex
}

// 生成一个随机数
export const randomNum = (min, max) => {
  return Math.floor(Math.random() * (max - min + 1)) + min
}

export const timeTran = (time) => {
  const toDay = new Date(time)

  let hour = toDay.getHours() < 10 ? '0' + toDay.getHours() : toDay.getHours() //时
  let minute =
    toDay.getMinutes() < 10 ? '0' + toDay.getMinutes() : toDay.getMinutes() //分
  let second =
    toDay.getSeconds() < 10 ? '0' + toDay.getSeconds() : toDay.getSeconds() //秒

  const nowTime = hour + +minute + +second

  return nowTime
}
